<template>
  <div>
    <!-- 面包屑部分 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table style="width: 100%" border stripe :data="rolesList" row-key="id">
        <el-table-column type="expand">
          <!-- 展开行部分 -->
          <template v-slot="scope">
            <!-- 一级权限 -->
            <el-row v-for="(item1, i1) in scope.row.children" :key="item1.id" :class="[ i1 === 0 ? 'bdtop' : '' , 'bdbom', 'cen']">
              <!-- 第一列，一级权限名称 -->
              <el-col :span="4">
                <el-tag type="success" closable @close="removeRight(scope.row, item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>

              <el-col :span="20">
                <el-row v-for="(item2, i2) in item1.children" :key="item2.id" :class="[ i2 === 0 ? '' : 'bdtop', 'cen' ]">
                  <!-- 第二列，二级权限名称 -->
                  <el-col :span="6">
                    <el-tag type="primary" closable @close="removeRight(scope.row, item2.id)">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag style="margin: 5px 10px;" closable type="danger" v-for="item3 in item2.children" :key="item3.id" @close="removeRight(scope.row, item3.id)">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <div>
              <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
              <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
              <el-button type="warning" size="mini" icon="el-icon-setting" @click="rightsSet(scope.row)">分配权限</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="rightSetDialogVisible"
      width="50%"
      @close="handleClose">
      <el-tree
      ref="rightSetRef"
        :data="rightList"
        :props="props"
        node-key="id"
        show-checkbox
        default-expand-all
        :default-checked-keys="defaultKeys">
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightSetDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="rightSetBtn">确 定</el-button>
      </span>
    </el-dialog>
    </div>
</template>

<script>
export default {
  data () {
    return {
      rolesList: [],
      // 分配权限对话框
      rightSetDialogVisible: false,
      // 权限列表
      rightList: [],
      // 树形配置项
      props: {
        label: 'authName',
        children: 'children'
      },
      // 默认选中项数组
      defaultKeys: [],
      // 保存需要分配权限的角色ID
      rolesId: ''
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) return this.$message.error('获取角色列表失败')
      this.rolesList = res.data
    },
    // 删除权限
    async removeRight (role, rightid) {
      const confirm = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirm === 'confirm') {
        const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightid}`)
        if (res.meta.status !== 200) return this.$message.error('删除权限失败')
        // this.getRolesList()
        role.children = res.data
      }
    },
    // 分配权限
    async rightsSet (role) {
      // 保存ID
      this.rolesId = role.id
      console.log(role)
      // 获取所有权限列表
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) return this.$message.error('获取权限列表失败')
      this.rightList = res.data
      this.getDefaultKeys(role, this.defaultKeys)
      this.rightSetDialogVisible = true
    },
    // 定义筛选默认选中项的方法
    getDefaultKeys (data, arr) {
      if (!data.children) {
        return arr.push(data.id)
      }
      data.children.forEach(item => {
        this.getDefaultKeys(item, arr)
      })
    },
    handleCheckChange () {},
    // 分配权限对话框的关闭事件
    handleClose () {
      this.defaultKeys = []
      this.rightSetDialogVisible = false
    },
    // 分配权限的提交事件
    async rightSetBtn () {
      const key = [...this.$refs.rightSetRef.getCheckedKeys(), ...this.$refs.rightSetRef.getHalfCheckedKeys()]
      const rids = key.join(',')
      const { data: res } = await this.$http.post(`roles/${this.rolesId}/rights`, { rids: rids })
      if (res.meta.status !== 200) return this.$message.error('分配权限失败')
      this.$message.success('分配权限成功')
      this.getRolesList()
      this.rightSetDialogVisible = false
    }
  }
}
</script>

<style scoped lang="less">
.bdtop {
  border-top: 1px solid #eee;
}
.bdbom {
  border-bottom: 1px solid #eee;
}
.cen {
  display: flex;
  align-items: center;
}
</style>
